// $Id: oaxaca.h 458 2014-03-13 00:30:11Z alan.watson.f $

#if __cplusplus
extern "C" {
#endif

#include <stdio.h>

#include <sys/types.h>

typedef struct {
  int sx, sy;
  int nx, ny;
} oaxaca_window_t;

#define OAXACA_DETECTOR_INDEX_WFS        0
#define OAXACA_DETECTOR_INDEX_FINDER     1
#define OAXACA_DETECTOR_INDEX_INSTRUMENT 2
#define OAXACA_DETECTOR_INDEX_MAX        2

typedef struct {
  int initialized;
  int abort;
  double scale;
  double pixelfrequency;
  int cx;
  int cy;
  oaxaca_window_t window;
  double minframefrequency, maxframefrequency;
  double framefrequency;
  double actualframefrequency;
  double frametime;
  unsigned long frame;
  unsigned long framesperintegration;
  double integrationtime;
  unsigned long integration;
  unsigned long integrationsperexposure;
  double exposuretime;
  double zmin;
  double zmax;
  double zmean;
  double fwhm;
  double eshm;
  double spatialsigma;
  double temporalsigma;
  int searchradius;
  int refinementradius;
  int ns;
  double r[OAXACA_NS_MAX];
  double s[OAXACA_NS_MAX];
  double dsmean[OAXACA_NS_MAX];
  double rmsdsmean;
  double dssigma[OAXACA_NS_MAX];
  double rmsdssigma;
  double hartmannfwhm;
  double i_matrix[OAXACA_NA_MAX][OAXACA_NS_MAX];
  double c_matrix[OAXACA_NA_MAX][OAXACA_NS_MAX];
  double g[OAXACA_NA_MAX];
  double timestamp;
  double totaldelay;
} oaxaca_detector_t;

typedef struct oaxaca_shm {
  oaxaca_detector_t detector[OAXACA_DETECTOR_INDEX_MAX + 1];
  struct {
    int initialized;
    int command;
  } finder;
  struct {
    int initialized;
    double cpu_usage;
    double disk_usage;
  } housekeeping;
  struct {
    int initialized;
    int command;
  } instrument;
  struct {
    int initialized;
    int command;
    double timestamp;
    double totaldelay;
    double tmv[OAXACA_TM_NV];
    double dmv[OAXACA_DM_NV];
    double a[OAXACA_NA_MAX];
    double aflat[OAXACA_NA_MAX];
    double amean[OAXACA_NA_MAX];
    double asigma[OAXACA_NA_MAX];
    double damean[OAXACA_NA_MAX];
    double dasigma[OAXACA_NA_MAX];
    double asaturated[OAXACA_NA_MAX];
  } wfc;
  struct {
    int initialized;
    int command;
  } wfs;
  struct {
    int initialized;
    int command;
    int state;
  } lamp;
  struct {
    double interval;
    double sigma;
    double K;
    double r0;
    double lambda;
    double fwhm;
  } dimm;
  struct {
    double alpha;
    double delta;
    double ha;
    double zd;
  } telescope;
} oaxaca_shm_t;

extern volatile oaxaca_shm_t * oaxaca_shm;
extern const char *oaxaca_shm_initialize(void);

extern FILE *oaxaca_stderr;
extern void oaxaca_debug(const char *, ...);
extern void oaxaca_debug_simple(const char *);
extern void oaxaca_info(const char *, ...);
extern void oaxaca_info_simple(const char *);
extern void oaxaca_error(const char *, ...);
extern void oaxaca_error_simple(const char *);
extern void oaxaca_common_main(const char *);
extern void oaxaca_exit(int);

extern void oaxaca_sleep(long);
extern void oaxaca_usleep(long);

extern const char *oaxaca_initialize(void);

extern void oaxaca_detector_set_abort(int, int);
extern double oaxaca_detector_scale(int);
extern void oaxaca_detector_set_scale(int, double);
extern double oaxaca_detector_pixelfrequency(int);
extern void oaxaca_detector_set_pixelfrequency(int, double);
extern int oaxaca_detector_window_cx(int);
extern void oaxaca_detector_set_window_cx(int, int);
extern int oaxaca_detector_window_cy(int);
extern void oaxaca_detector_set_window_cy(int, int);
extern int oaxaca_detector_window_sx(int);
extern void oaxaca_detector_set_window_sx(int, int);
extern int oaxaca_detector_window_sy(int);
extern void oaxaca_detector_set_window_sy(int, int);
extern int oaxaca_detector_window_nx(int);
extern void oaxaca_detector_set_window_nx(int, int);
extern int oaxaca_detector_window_ny(int);
extern void oaxaca_detector_set_window_ny(int, int);
extern double oaxaca_detector_minframefrequency(int);
extern double oaxaca_detector_maxframefrequency(int);
extern double oaxaca_detector_framefrequency(int);
extern void oaxaca_detector_set_framefrequency(int, double);
extern double oaxaca_detector_actualframefrequency(int);
extern double oaxaca_detector_frametime(int);
extern unsigned long oaxaca_detector_frame(int);
extern unsigned long oaxaca_detector_framesperintegration(int);
extern double oaxaca_detector_integrationtime(int);
extern void oaxaca_detector_set_integrationtime(int, double);
extern unsigned long oaxaca_detector_integration(int);
extern unsigned long oaxaca_detector_integrationsperexposure(int);
extern double oaxaca_detector_exposuretime(int);
extern void oaxaca_detector_set_exposuretime(int, double);
extern double oaxaca_detector_zmin(int);
extern double oaxaca_detector_zmax(int);
extern double oaxaca_detector_zmean(int);
extern double oaxaca_detector_fwhm(int);
extern double oaxaca_detector_eshm(int);
extern double oaxaca_detector_spatialsigma(int);
extern double oaxaca_detector_temporalsigma(int);
extern unsigned long oaxaca_detector_search_radius(int);
extern void oaxaca_detector_set_search_radius(int, unsigned long);
extern unsigned long oaxaca_detector_refinement_radius(int);
extern void oaxaca_detector_set_refinement_radius(int, unsigned long);
extern unsigned long oaxaca_detector_ns(int);
extern void oaxaca_detector_set_ns(int, unsigned long);
extern double oaxaca_detector_s_vector_ref(int, unsigned long);
extern double oaxaca_detector_r_vector_ref(int, unsigned long);
extern void oaxaca_detector_r_vector_set(int, unsigned long, double);
extern double oaxaca_detector_ds_mean_vector_ref(int, unsigned long);
extern double oaxaca_detector_ds_sigma_vector_ref(int, unsigned long);
extern double oaxaca_detector_dt_sigma(int);
extern double oaxaca_detector_i_matrix_ref(int, unsigned long, unsigned long);
extern void oaxaca_detector_i_matrix_set(int, unsigned long, unsigned long, double);
extern double oaxaca_detector_c_matrix_ref(int, unsigned long, unsigned long);
extern void oaxaca_detector_c_matrix_set(int, unsigned long, unsigned long, double);
extern double oaxaca_detector_g_vector_ref(int, unsigned long);
extern void oaxaca_detector_g_vector_set(int, unsigned long, double);

extern void oaxaca_finder_main(void);
extern int oaxaca_finder_initialized(void);
extern char oaxaca_finder_command(void);
extern void oaxaca_finder_set_command(char);
extern int oaxaca_finder_detector_index(void);

extern void oaxaca_housekeeping_main(void);
extern int oaxaca_housekeeping_initialized(void);
extern double oaxaca_housekeeping_cpu_usage(void);
extern double oaxaca_housekeeping_disk_usage(void);

extern void oaxaca_html_main(void);

extern void oaxaca_instrument_main(void);
extern int oaxaca_instrument_initialized(void);
extern char oaxaca_instrument_command(void);
extern void oaxaca_instrument_set_command(char);
extern int oaxaca_instrument_detector_index(void);

extern void oaxaca_wfc_main(void);
extern int oaxaca_wfc_initialized(void);
extern char oaxaca_wfc_command(void);
extern void oaxaca_wfc_set_command(char);
extern unsigned long oaxaca_wfc_cycle(void);
extern double oaxaca_wfc_a_vector_ref(unsigned int);
extern void oaxaca_wfc_a_vector_set(unsigned int, double);
extern double oaxaca_wfc_a_flat_vector_ref(unsigned int);
extern void oaxaca_wfc_a_flat_vector_set(unsigned int, double);
extern double oaxaca_wfc_a_mean_vector_ref(unsigned int);
extern double oaxaca_wfc_a_sigma_vector_ref(unsigned int);
extern double oaxaca_wfc_da_mean_vector_ref(unsigned int);
extern double oaxaca_wfc_da_sigma_vector_ref(unsigned int);
extern double oaxaca_wfc_tm_v_vector_ref(unsigned int);
extern double oaxaca_wfc_dm_v_vector_ref(unsigned int);

extern void oaxaca_wfs_main(void);
extern int oaxaca_wfs_initialized(void);
extern char oaxaca_wfs_command(void);
extern void oaxaca_wfs_set_command(char);
extern int oaxaca_wfs_detector_index(void);

extern void oaxaca_lamp_main(void);
extern int oaxaca_lamp_initialized(void);
extern char oaxaca_lamp_command(void);
extern void oaxaca_lamp_set_command(char);
extern int oaxaca_lamp_state(void);

#if __cplusplus
}
#endif
